<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">

            <el-form-item label="所属行业" prop="industryId" style="width: 200px">
                <el-select v-model="queryParams.industryId" placeholder="请输入所属行业" filterable clearable>
                    <el-option v-for="item in listIndustryArr" :key="item.industryId" :label="item.name"
                        :value="item.industryId" />
                </el-select>
            </el-form-item>


            <el-form-item label="所属系统" prop="systemName">
                <el-input v-model="queryParams.systemName" placeholder="请输入所属系统" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="所属区域" prop="areaId" style="width: 200px">
                <el-cascader class="flex-1" v-model="queryParams.areaId" clearable :options="options" :props="props2"
                    @change="TreeChange1" />
            </el-form-item>
            <el-form-item label="商家名称" prop="legalPersonName">
                <el-input v-model="queryParams.legalPersonName" placeholder="请输入姓名" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="法人姓名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="联系电话" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <!--  -->
        <el-table border v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="序号" align="center" type="index" width="55">
                <template #default="{ $index }">
                    {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="商家名称" align="center" prop="name" />
            <el-table-column label="门面照片" width="100" align="center" prop="doorFace">
                <template #default="scope">
                    <el-image style="width: 50px; height: 50px;" :src="scope.row.doorFace"
                        :preview-src-list="[scope.row.doorFace]" preview-teleported>
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column label="商家类型" align="center" prop="type">
                <template #default="scope">
                    <span v-if="scope.row.type == 1">独立</span>
                    <span v-if="scope.row.type == 2">加盟</span>
                    <span v-if="scope.row.type == 3">连锁</span>
                </template>
            </el-table-column>
            <el-table-column label="软件版本" align="center" prop="merchantAppVersionName" />
            <el-table-column label="使用期限" align="center" prop="merchantAppVersionExpire" />

            <el-table-column label="所属行业" align="center" prop="industryName" />
            <el-table-column label="所属系统" align="center" prop="systemName" />
            <el-table-column label="所属区域" align="center" prop="addressArea" />
            <el-table-column label="单位地址" align="center" prop="addressDetail" />

            <el-table-column label="法人姓名" align="center" prop="legalPersonName" />
            <el-table-column label="法人电话" align="center" prop="legalPersonPhone" />
            <el-table-column label="单位电话" align="center" prop="phone" />
            <el-table-column label="邮箱" align="center" prop="email" />

            <el-table-column label="创建时间" align="center" prop="createTime">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    <dict-tag :options="merchant_apply_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="操作人员" align="center" prop="updateUserName" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" v-if="scope.row.status == 0" icon="Edit"
                        @click="handleExamine(scope.row)">审核</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改员工对话框 -->
        <el-dialog :title="title" v-model="open" width="800px" append-to-body>
            <el-form :model="form" :rules="rules" ref="noticeRef" label-position="" label-width="150px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="法人身份证号" prop="legalPersonIdcardNum">
                            <el-input v-model="form.legalPersonIdcardNum" placeholder="法人身份证号" maxlength="30" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="法人姓名" prop="legalPersonName">
                            <el-input v-model="form.legalPersonName" placeholder="法人姓名" maxlength="30" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="法人手机号" prop="legalPersonPhone">
                            <el-input v-model="form.legalPersonPhone" placeholder="法人手机号" maxlength="30" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="商家名称" prop="name">
                            <el-input v-model="form.name" placeholder="商家名称" maxlength="30" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="办公场所" prop="officeSpace">
                            <el-input v-model="form.officeSpace" placeholder="办公场所" maxlength="30" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="办公场所电话" prop="officeSpacePhone">
                            <el-input v-model="form.officeSpacePhone" placeholder="办公场所电话" maxlength="30" />
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-row>
                    <el-col :span="12">
                        <el-form-item label="地址地区" prop="addressArea">
                            <el-input v-model="form.addressArea" placeholder="请输入地址地区" maxlength="30" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="地址详情" prop="addressDetail">
                            <el-input v-model="form.addressDetail" placeholder="请输入地址详情" maxlength="30" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请人学历证书" prop="applicantEduCert">
                            <el-image :src="form.applicantEduCert" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请人职业许可证" prop="applicantOccupationCert">
                            <el-image :src="form.applicantOccupationCert" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请人其他证件" prop="applicantOtherCert">
                            <el-image :src="form.applicantOtherCert" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请人职称证书" prop="applicantProfessionCert">
                            <el-image :src="form.applicantProfessionCert" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="营业执照" prop="businessLicense">
                            <el-image :src="form.businessLicense" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="医师执业许可证" prop="doctorLicense">
                            <el-image :src="form.doctorLicense" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="门面照" prop="doorFace">
                            <el-image :src="form.doorFace" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="消防安全许可证" prop="fireSafetyCertificate">
                            <el-image :src="form.fireSafetyCertificate" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="法人身份证正面" prop="legalPersonIdcardFront">
                            <el-image :src="form.legalPersonIdcardFront" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="法人身份证反面" prop="legalPersonIdcardBack">
                            <el-image :src="form.legalPersonIdcardBack" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="其他相关证件" prop="otherCertificate">
                            <el-image :src="form.otherCertificate" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="药师执业许可证" prop="pharmacistLicense">
                            <el-image :src="form.pharmacistLicense" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="行业许可证" prop="industryLicense">
                            <el-image :src="form.industryLicense" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="食品安全许可证" prop="foodSafetyCertificate">
                            <el-image :src="form.foodSafetyCertificate" preview-teleported
                                :preview-src-list="form.foodSafetyCertificate" />
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">同 意</el-button>
                    <el-button @click="cancel">拒 绝</el-button>
                </div>
            </template>
        </el-dialog>


    </div>
</template>

<script setup name="Notice">
// 获取行业
import {
    listIndustry
} from "@/api/backIndustry/backIndustry.js";
let listIndustryArr = ref([])
listIndustry().then((res) => {
    listIndustryArr.value = res.records
})

// 获取地区
import {
    getAreaTree
} from "@/api/common";
let options = ref([])
getAreaTree().then((res) => {
    options.value = res
})

const props2 = { multiple: false, value: "areaId", label: "name", emitPath: true, checkStrictly: true }
function TreeChange1(e) {
    data.queryParams.areaId = `${e[e.length - 1]}`
}

import {
    applyDetail,
    applyCheck,
    listMerchantApply,
} from "@/api/backMerchant/backMerchant";
const { proxy } = getCurrentInstance();
const { occupation_status, merchant_apply_status } = proxy.useDict(
    "occupation_status",
    "merchant_apply_status"
)

const sys_normal_disable = [
    {
        elTagClass: "",
        elTagType: "primary",
        label: "是",
        value: true,
    },
    {
        elTagClass: "",
        elTagType: "danger",
        label: "否",
        value: false,
    },
];

const roleOptions = ref([]);
const deptList = ref([]);
const noticeList = ref([]);

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        currentPage: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        status: undefined,
    },
    rules: {
        // name: [
        //     { required: true, message: "姓名不能为空", trigger: "blur" },
        // ],
    },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询员工列表 */
function getList() {
    loading.value = true;
    listMerchantApply(queryParams.value).then((response) => {
        noticeList.value = response.records;
        console.log(noticeList.value, "noticeList.value");

        total.value = response.total;
        loading.value = false;
    });
}

/** 表单重置 */
function reset() {
    form.value = {
        "deptId": 0,
        "roleIds": 0,
        "employeeId": 0,
        "isAdmin": undefined,
        "isBandh": undefined,
        "name": "",
        "occupation": undefined,
        "position": "",
        "profession": "",
    };
    proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.currentPage = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.employeeId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加员工";
}

/**修改按钮操作 */
function handleUpdate(row) {
    reset();
    const employeeId = row.employeeId || ids.value;
    // getNotice(employeeId).then((response) => {
    //     form.value = response;
    //     roleOptions.value = response.roles
    //     deptList.value = response.deptList
    //     open.value = true;
    //     title.value = "修改员工";
    // });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const employeeIds = row.employeeId || ids.value;
    proxy.$modal
        .confirm('是否确认删除员工编号为"' + employeeIds + '"的数据项？')
        .then(function () {
            // return delNotice(employeeIds);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
}



/** 取消按钮 */
function cancel() {
    let data = {
        merchantId: form.value.merchantId,
        status: 2,
    }
    open.value = false;
    applyCheck(data).then((response) => {
        proxy.$modal.msgSuccess("拒绝成功");
        open.value = false;
        getList();
    });
    reset();
}

/** 提交按钮 */
function submitForm() {
    let data = {
        merchantId: form.value.merchantId,
        status: 1,
    }
    applyCheck(data).then((response) => {
        proxy.$modal.msgSuccess("同意成功");
        open.value = false;
        getList();
    });
}

/* 审核 */
async function handleExamine(row) {
    reset();
    const merchantId = row.merchantId || ids.value;
    applyDetail(merchantId).then((response) => {
        form.value = response;
        roleOptions.value = response.roles
        deptList.value = response.deptList
        open.value = true;
        title.value = "审核商家";
    });
}

getList();
</script>

<style scoped>
.el-image {
    width: 200px;
    height: 60px;
}
</style>